import 'package:flutter/material.dart';
import 'package:aweb5/base/BaseWidget.dart';
import 'package:dio/dio.dart';

class HttpUtil extends BaseWidget {
  static Dio _dio;
  static BaseOptions _options = new BaseOptions(
    connectTimeout: 5000,
    receiveTimeout: 3000,

  );

  static get(String url, {options, Function success, Function failure}) async {
    Dio dio = buildDio();
    try {
      Response response = await dio.get(url, options: options);
      success(response.data);
    } catch (exception) {
      failure(exception);
    }
  }

  static post(String url,
      {params, options, Function success, Function failure}) async {
    Dio dio = buildDio();
    try {
      Response response = await dio.post(url, data: params, options: options);
      success(response.data);
    } catch (exception) {
      failure(exception);
    }
  }

  static Dio buildDio() {
    if (_dio == null) {
      _dio = new Dio(_options);
    }
    return _dio;
  }
}